function removeRow(elem) {
    ids = document.getElementById('form_ids');
    var par = elem.parentNode.parentNode;
    removeChildren(par);
    beg = elem.name.lastIndexOf('_');
    end = elem.name.length;
    id = elem.name.substring(beg+1,end);
    removeIds(id);
}
function removeChildren(elem) {
  while(elem.lastChild) {
    elem.removeChild(elem.lastChild);
  }
}
function removeIds(id) {
    
    ids = document.getElementById('form_ids');
    if (ids) {
	var lst= ids.value.split(',');
	var ind = lst.indexOf(id);
	lst.splice(ind,1);
	ids.value = lst.join(',');
	
    if (ids.value=='') 
	document.location.href = '.';
    }
}
function collectIDs(prefix, hidden_ids) {
    ids = new Array();
    var allels = document.getElementsByTagName('input');
    for (i = 0; i < allels.length; i++) {
        var elem = allels[i];
        if(elem.type=="checkbox" && elem.name && elem.name.indexOf(prefix) == 0) {
            if(elem.checked) {
                obj_id = elem.name.replace(prefix, '');
                ids.push(obj_id);
            }
        }
    }
    return ids.join(',');
}

function runAction(submit_id,checkbox_prefix, hidden_ids, hidden_ma_name, xxx) { 	 	 
    ids = collectIDs(checkbox_prefix);
    if (ids.length==0) {
        alert(no_items_selected_message);
	return false;
    }
    ma_elem = document.getElementById(hidden_ma_name);
    if (ma_elem) {
	ma_elem.value = submit_id;
    }
    
    ids_elem = document.getElementById(hidden_ids)
    if (ids_elem) 
	ids_elem.value = ids;
                       
    return true;
}

    
function toggleAll(prefix, checked){
    var allels = document.getElementsByTagName('input');
    for(var i = 0; i < allels.length; i++) {
        var elem = allels[i];
        if(elem.type=="checkbox" && elem.name && elem.name.indexOf(prefix) >= 0) {
            elem.checked = checked;
	    changeRowColor(elem);
        }
    }
}

function toggleDisplay(id) {
    elem = document.getElementById(id);
    if (elem) 
       elem.style.display = (elem.style.display == 'block') ? 'none' : 'block';
}

function showElement(id) {
    changeDisplay(id, null);
}

function hideElement(id) {
    changeDisplay(id, 'none');
}

function changeDisplay(id, style) {
    if (style == undefined)
         style = null;

    elem = document.getElementById(id);
    if (elem)
        elem.style.display = style;
         
}


function changeRowColor(check_box){
    var bg;
    if (check_box.checked) 
        bg = "#D2DCE6";
    else 
        bg = "white";
        
    var className = check_box.className
    if (className){
          var elems = document.getElementsByClassName(className);
          for (var i = 0; i < elems.length; i++)
                    elems[i].style.backgroundColor = bg;
    }
}


function setAllByRegExpFromElem(elemId,prefix, posfix ) {
    var elem = document.getElementById(elemId);
    if (!elem)
        return;
    var beg = '';
    var end ='';
    
    if (!(prefix==undefined || !prefix))
       beg = '^' +prefix +'_';
    if (!(posfix==undefined || !posfix))
        end = '_'+ posfix+'$';
    expression = beg+'\\d+' +end;
    regExp = new RegExp(expression);
    
    var allels = document.getElementsByTagName(elem.tagName);
    for (i = 0; i < allels.length; i++) 
        if(allels[i].id && regExp.test(allels[i].id)) {
            allels[i].value = elem.value;
        }
}

function setAllAutoCompleteField(name) {
    setAllByRegExpFromElem('apply_'+name+'_text',name,'text'  );
    setAllByRegExpFromElem('apply_'+name+'_hidden',name,'hidden'  )
}

function setAllDefault(name) {
    setAllByRegExpFromElem('apply_'+name,name);
}

function setAllCheckbox(name) {
    var elem = document.getElementById('apply_'+name);
    if (!elem || elem.type!="checkbox")
        return;
    toggleAll(name,elem.checked);
}


function openFrame(href, id){
    var node = document.getElementById(id);
    node.style.display = 'block';
    node.src = href;
        
}
function openInfo(controller_name, hiddenID, type)
{
    var elem=document.getElementById(hiddenID);
    if (!(elem&&elem.value))
       return;
    var href='/'+controller_name+'/'+elem.value + type;
    openFrame(href, 'ifr');
    
}
function closeInfo(){
            var node = window.parent.document.getElementById('info')
            node.style.display = 'none';
}
function closeFrame(){
    if(window!=top){
        if( !((window.location.href.indexOf('/add')>=0)||
	      (window.location.href.indexOf('/edit')>=0)||
	      (window.location.href.indexOf('/show')>=0))){
            var node = window.parent.document.getElementById('ifr')
            node.style.display = 'none';
        }
    }
}
closeFrame();

function cancel()
{
    if(window==top)
        history.back();
    else
        window.parent.document.getElementById('ifr').style.display = 'none';
}
function hideDiv()
{
    if (window!=top)
        if ((window.location.href.indexOf('/add')>=0)||(window.location.href.indexOf('/edit')>=0))
            document.body.className='invisible';
        else
            document.body.className='invisible info';
    else document.body.className='';
}	